package com.five.admin.service.impl;

import com.five.admin.dao.UserMapper;
import com.five.entity.User;
import com.five.admin.service.UserService;
import com.five.exceptions.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-18 21:36
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private  UserMapper userMapper;

    /**
     * 获取所有用户信息
     * @return 返回所有用户信息
     */
    @Override
    public List<User> getAllUser() {
        List<User> users = userMapper.selectAllUser();
        return users;
    }

    /**
     * 通过user对象查询user
     * @param user user对象
     * @return 返回user对象
     */
    @Override
    public User getUser(User user) {
        User getUser = userMapper.selectUser(user);
        return getUser;
    }

    /**
     * 通过id删除用户信息
     * @param user 用户id
     */
    @Override
    public void deleteUser(User user) {
        userMapper.deleteUser(user);
    }

    /**
     * 根据user对象修改数据库用户信息
     * @param user
     */
    @Override
    public void updateUser(User user) {
        User selectUser = new User();
        selectUser.setUsername(user.getUsername());
        selectUser.setId(user.getId());
        User resultUser = userMapper.selectByUserNameExceptSelfId(selectUser);
        if (resultUser!=null){
            throw new BusinessException("用户名重复");
        }
        userMapper.updateUser(user);
    }

    /**
     * 新增用户
     * @param user
     */
    @Override
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public List<User> fuzzyQueryByNickName(String nickname) {
        List<User> users = userMapper.fuzzyQueryByNickName(nickname);
        return users;
    }

    /**
     * 管理员登录(根据电话和密码查询管理员)
     * @param telephone
     * @return
     */
    @Override
    public User selectAdmin(Long telephone) {
        User getUser = userMapper.selectAdmin(telephone);
        System.out.println(getUser);
        return getUser;
    }
}
